/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.db;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.builder.annotation.MapperAnntBuilder;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.mapperdb.core.Assert;

public class Config {
	String name;
	SqlSessionFactory sqlSessionFactory;
	MapperAnntBuilder mapperAnntBuilder;
	Map<Class<?>, ResultEntity> resultEntityMap = new HashMap<Class<?>, ResultEntity>();

	Config() {}

	/**
	 * Constructor with DataSource
	 * @param dataSource the dataSource, can not be null
	 */
	public Config(String name, SqlSessionFactory sqlSessionFactory) {
		Assert.hasLength(name, "Config name can not be blank");
		Assert.notNull(sqlSessionFactory, "SqlSessionFactory can not be null");
		this.name = name.trim();
		this.sqlSessionFactory = sqlSessionFactory;
	}

	public SqlSession getSqlSession() {
		return sqlSessionFactory.openSession();
	}

	public String getName() {
		return name;
	}

	public SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public Configuration getConfiguration() {
		return sqlSessionFactory.getConfiguration();
	}

	public void addResultEntity(ResultEntity resultEntity) {
		resultEntityMap.put(resultEntity.entityClass(), resultEntity);
	}

	public ResultEntity getResultEntity(Class<?> entityClass) {
		return resultEntityMap.get(entityClass);
	}

	public MapperAnntBuilder getMapperAnntBuilder() {
		return mapperAnntBuilder;
	}

	public static Config newConfig(Config config, SqlSession sqlSession) {
		Assert.notNull(config, "Config can not be null");
		Assert.notNull(sqlSession, "SqlSession can not be null");
		Assert.isTrue(config.getConfiguration().equals(sqlSession.getConfiguration()), "SqlSession not in " + config.getName());
		return new SqlSessionConfig(config, sqlSession);
	}

	static class SqlSessionConfig extends Config {
		SqlSession sqlSession;

		public SqlSessionConfig(Config config, SqlSession sqlSession) {
			super(config.getName(), config.getSqlSessionFactory());
			this.sqlSession = sqlSession;
		}

		@Override
		public SqlSession getSqlSession() {
			return sqlSession;
		}
	}
}
